<?php

namespace app\admin\business;

use app\common\business\BaseBus;
use app\common\model\mysql\SpecsValue as specsValueModel;
use think\Exception;

class SpecsValue extends BaseBus
{
    public $Model = null;

    public function __construct()
    {
        $this->Model = new specsValueModel();
    }
    public function getBySpecsId($data)
    {
        try {
            $res = $this->Model->getBySpecsId($data);
        } catch (\Exception $e) {
            throw new Exception("内部异常");
        }
        return $res->toArray();
    }


    public function getNamesBySpecsId($data)
    {
        $names = $this->Model->getNamesBySpecsId($data['specs_id']);
        $color = [];
        foreach ($names as $name) {
            $color[] =  $name['name'];
        }
        //        dump(in_array($data['name'], $color));
        if (in_array($data['name'], $color)) {
            return false;
        } else {
            return true;
        }
    }

    public function getById($id)
    {
        if (!$id) {
            throw new Exception("不存在该条记录");
        }
        $res = $this->Model->find($id);
        if (!$res) {
            return [];
        }
        return $res->toArray();

    }

    public function getStatus($id, $status)
    {
        $res = $this->getById($id);
        if (!$res) {
            throw new Exception("不存在该记录");
        }
        if ($res['status'] == $status) {
            throw new Exception("更改前后状态一致");
        }
        $result = $this->Model->updateByStatus($id, $status);
        if (!$result) {
            return false;
        } else {
            return true;
        }
    }
}